﻿# -*- coding: utf-8 -*-
import requests
import re
import json
import os
import sys
import datetime
import readline
import time
import subprocess

def validate(date_text):
    try:
        datetime.datetime.strptime(date_text, '%Y-%m-%d')
    except ValueError:
        raise ValueError("Incorrect data format, should be YYYY-MM-DD")

def get_rdb_file():
    # 压缩包确认
    if os.path.isfile("/data/offline_debug.tar.gz"):
        choice = input('please make sure:the current offline_debug.tar.gz is u need (y/n)?')
        if choice == "y":
            os.system("cd /data && tar zxf offline_debug.tar.gz >/dev/null 2>&1 && echo 'unpack offline_debug.tar.gz finish' ")
        elif choice == "n":
            time.sleep(1)
            print('please upload the new offline_debug.tar.gz')
            time.sleep(1)
            os.system("cd /data && rz -y")
            if os.path.isfile("/data/offline_debug.tar.gz"):
                cho = input('find the offline_debug.tar.gz still want to unpack it(y/n)?')
                if cho == "y":
                    os.system("cd /data && tar zxf offline_debug.tar.gz >/dev/null 2>&1 && echo 'unpack offline_debug.tar.gz finish' ")
                elif cho == "n":   
                    print("there is nothing to do!")
                    sys.exit() 
                else:
                    print("unrecongised cmd!")
                    sys.exit()     
            else:
                print("there is no offline_debug.tar.gz!")
                sys.exit()  
        else:
            print("unrecongised cmd!")
            sys.exit() 
    else:
        time.sleep(1)
        print('please upload the new offline_debug.tar.gz')
        time.sleep(1)
        os.system("cd /data && rz -y") 
        if os.path.isfile("/data/offline_debug.tar.gz"):
            os.system(" echo 'upload offline_debug.tar.gz success' ") 
            os.system("cd /data && tar zxf offline_debug.tar.gz >/dev/null 2>&1 && echo 'unpack offline_debug.tar.gz finish' ")
        else:
            print("there is no offline_debug.tar.gz!")
            sys.exit()  

def rdb_reload():
    datebase = ['host', 'cap', 'ce_fwd_global', 'ce_global', 'cfg_cp', 'cfg_can', 'cfg_global', 'ctrl_global', 'ctrl_local', 'de_fwd_global', 'de_global', 'de_standalone', 'host_fwd', 'ibc_global', 'monitor_global', 'ms_fwd_global', 'ms_global', 'dev_global']
    for db in datebase:
        print('redis_'+db+'.sock')
        enter_database = 'source /home/x86rgos/x86-env-setup;redis-cli -s /tmp/redis/redis_'+db+'.sock'
        p = subprocess.Popen([enter_database],shell=True,executable='/bin/bash',stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        x = 'rdbreload /data/offline_debug/tmp_redis/redis_'+db+'.rdb 0\n'
        p.stdin.write(x.encode())
        quit_database = 'quit\n'
        p.stdin.write(quit_database.encode())
        output = p.communicate()[0]
        out = bytes.decode(output)
        print(out) 

def images_update(imagedb_url, image_url, imagepd_deb, imagepd_deb2):
    os.system("cd /home/ && wget -N http://rcode.ruijie.work/updateMOM_v2.0.sh  >/dev/null 2>&1 &&" +
              "wget -N http://rcode.ruijie.work/updateMOMcfg_v2.0.sh  >/dev/null 2>&1 &&" +
              "wget -N http://rcode.ruijie.work/updateMOMs6120product_v2.0.sh   >/dev/null 2>&1")
    os.system("cd /home/ && sh updateMOM_v2.0.sh " + imagedb_url + " && echo 'update pd finish' ")
    os.system("cd /home/ && sh updateMOMcfg_v2.0.sh " + image_url + " && echo 'update rgos finish' ")
    os.system("cd /home/ && sh updateMOMs6120product_v2.0.sh " + imagepd_deb + " && echo 'update deb finish' ")
    os.system("cd /home/ && sh updateMOMs6120product_v2.0.sh " + imagepd_deb2 + " && echo 'update deb finish' ")

def start_mom():
    os.system("cd /home/ && sh startMOM.sh && echo 'start mom finish' ")

if __name__ == '__main__':

        requests.packages.urllib3.disable_warnings() 
        #产品号输入
        product_number = input('please input product number(12.1spl1 or 12.1spl2):')
        if product_number == "12.1spl1":    
            pd_treeName = 'git-build-sw'
            pd_branchName = '12_1_SPL1'
            pd_product = 'switch'
            pd_build_type = '完整编译-忽略错误'
            pd_build_typeid = 'M'
            cap_deb='rg_dev_def_u_cap_0.9.0_x86.deb'
            pi_treeName = 'git-build-platform'
            pi_branchName = '12_1_PL1'
            pi_product = 'rgos_x86'
            pi_build_type = '完整编译'
            pi_taskAttr = 'x86_4.16'
            pi_build_typeid = 'A'
        elif product_number == "12.1spl2":
            pd_treeName = 'git-build-sw'
            pd_branchName = '12_1_SPL2'
            pd_product = 'switch'
            pd_build_type = 'GCOV忽略错误'
            pd_build_typeid = 'GM'
            pi_treeName = 'git-build-platform'           
            pi_branchName = '12_1_PL2'
            pi_product = 'rgos'
            pi_build_type = '完整编译'
            pi_taskAttr = 'x86_4.16'
            pi_build_typeid = 'A'
            cap_deb='rg_dev_def_u_cap_99.0.0_x86.deb'
        else:
            print('wrong product number !!!')    
            sys.exit() 

        image_date = input('please input image date(forexample 2019-01-01):')
        validate(image_date)
        
        image_date_url = image_date.replace("-", '')
        update_flag = True

        if os.path.isfile("mom_url"):
            file = open('mom_url', 'r')
            text_line = file.readline()
            if text_line.find(image_date_url,0,len(text_line))!=-1:
                print("------------the date of images version is already on "+image_date+"------------")
                update_flag = False
            file.close()

        if(update_flag == True):  
            time.sleep(1)
            print("------------updating images-----------------")    
            #这边不需要添加cookie，后续的get访问中将cookie保存下来即可
            Request_Headers_cas = {
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
            'Referer' : 'https://sso.ruijie.net:8443/cas/login?service=http%3A%2F%2Fbuild.ruijie.net%3A8080%2Fngcf_build%2Fservlet%2F12index',
            'Connection' : 'keep-alive',
            'Host' : 'sso.ruijie.net:8443',
            'Origin' : 'https://sso.ruijie.net:8443',
            'Upgrade-Insecure-Requests' : '1',
            'Content-Type' : 'application/x-www-form-urlencoded',
            'Accept-Encoding' : 'gzip, deflate, br',
            'Accept-Language' : 'zh-CN,zh;q=0.9',
            'Cache-Control' : 'max-age=1000',
            'Content-Length' : '67'
            }

            #第一次通过get取得页面的lt参数，加入post当中，用于请求cas取回ticket
            url_to_cas = "https://sso.ruijie.net:8443/cas/login?service=http%3A%2F%2Fbuild.ruijie.net%3A8080%2Fngcf_build%2Fservlet%2FdailyBuildMain"
            response = requests.get(url_to_cas,verify=False)
            cookie = response.cookies
            list = re.findall(u'(?<=name="lt" value=").*?(?=" />)',response.text)
            lt = list.pop(0)

            Form_Data_cas = {
                    'username': 'libin3',
                    'password': '20122013wW',
                    'lt': lt,
                    '_eventId': 'submit',
                    'submit': ''
            }
            res_firstpost = requests.post(url=url_to_cas, headers=Request_Headers_cas, data=Form_Data_cas,cookies=cookie, allow_redirects=False, verify=False)
            cookie=res_firstpost.cookies
            #print("firstpost headers:"+res_firstpost.headers.__str__())

            url_with_ticket=res_firstpost.headers.get('Location')
            #print("url_with_ticket:"+url_with_ticket)
            Request_Headers_dest = {
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                    'Connection': 'keep-alive',
                    'host': 'build.ruijie.net:8080',
                    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                    #'Upgrade-Insecure-Requests:': '1'
            }
            #创建session，用于保持会话请求
            s=requests.session()

            #向带有ticket的url路径发送get请求返回的url中携带jsessionid参数
            res_firstget=s.get(url_with_ticket,headers=Request_Headers_dest,verify=False)
            if str(res_firstget.status_code) != '200':
                    print("error occurs in ticket")
                    logging.error('error occurs in ticket')
                    exit(1)
            #print("firstget url:"+res_firstget.url)
            cookie = res_firstget.cookies

            #获取jsessionid，以后访问cookie中携带可被系统识别
            jsessionid = re.search('build/servlet/dailyBuildMain;(.*)',res_firstget.url).group(1)
            #print(jsessionid)

            #携带jsessionid的cookie可以直接向访问的网址发送get请求
            res_secondget = s.get(res_firstget.url, cookies=cookie,verify=False)#使用的url路径中需要携带jession,get请求在url路径中携带参数
            cookie = res_secondget.cookies
            #print("res_secondget url:"+res_secondget.url)
            #print(res_secondget.text)
            url_to_dailybuild='http://build.ruijie.net:8080/ngcf_build/servlet/dailyBuild'
            form_data_forsearch = {
                    'start' : '0',
                    'limit' : '15',
                    'type' : 'query',
                    'queryBean.treeName' : pi_treeName,
                    'queryBean.branchName' : pi_branchName,
                    'queryBean.product' : pi_product,
                    'queryBean.buildTypeId' : pi_build_typeid,
                    'queryBean.buildType' : pi_build_type,
                    'queryBean.buildDate' : '',
                    'queryBean.queryStartTime' : image_date+'T00:00:00',
                    'queryBean.queryEndTime' : image_date+'T00:00:00',
                    'queryBean.result' : '[success]',
                    'queryBean.errorMsg' : '',
                    'queryBean.taskAttr' : pi_taskAttr,
                    'queryBean.owner' : '',
                    'queryBean.sort:' : 'id',
                    'queryBean.dir' : 'desc',
                    'queryBean.start:' : '0',
                    'queryBean.limit' : '15'
            }
            Reques_Headers_forsearch = {
                    'Accept': '*/*',
                    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                    'Referer': 'http://build.ruijie.net:8080/ngcf_build/servlet/initBuild?type=daily_build_query&project=git-rgosm-project',
                    'Connection': 'keep-alive',
                    'Host': 'build.ruijie.net:8080',
                    'Origin': 'http://build.ruijie.net:8080',
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Accept-Language': 'zh-CN,zh;q=0.9',
                    'X-Requested-With' : 'XMLHttpRequest',
                    'Content-Length' : '427'
            }

            #向服务器请求我们需要的数据并且以json格式返回
            res_secondpost=s.post(url_to_dailybuild,headers=Reques_Headers_forsearch,data=form_data_forsearch,allow_redirects=False,verify=False)
            if str(res_secondpost.status_code) != '200':
                    print("error occurs in search")
                    exit(1)

            #返回的json数据格式化，并且提取我们需要的location信息
            hjson = json.loads(res_secondpost.text)
            list = hjson['list']
            url_tag = "0"
            if len(list) >  0:
                list_dict = list[0]
                outputUrl = list_dict['outputUrl']
                print(outputUrl)
            else:
                print("there is no available pi images")
                sys.exit() 

            if url_tag == "0":
                    # 将字符串进行拼接得到我们需要的下载路径
                    des_url = re.search(r'.*(?=-)', outputUrl).group(0)
                    version_url = des_url + "-%5bdef%5d/rgos_info/rootfs/version"
                    image_url = des_url + "-%5bdef%5d/image.tar.bz2"
                    #print(version_url)
                    with open("mom_url", "w+") as file:
                            file.seek(0)
                            file.truncate()
                            file.write(image_url)
                            file.close()
            '''
            获取pd的下载路径
            '''
            form_data_forpd = {
                    'start' : '0',
                    'limit' : '15',
                    'type' : 'query',
                    'queryBean.treeName' : pd_treeName,
                    'queryBean.branchName' : pd_branchName,
                    'queryBean.product' : pd_product,
                    'queryBean.buildTypeId' : pd_build_typeid,
                    'queryBean.buildType' : pd_build_type,
                    'queryBean.buildDate' : '',
                    'queryBean.queryStartTime' : image_date+'T00:00:00',
                    'queryBean.queryEndTime' : image_date+'T00:00:00',
                    'queryBean.result' : '[success]',
                    'queryBean.errorMsg' : '',
                    'queryBean.taskAttr' : '',
                    'queryBean.owner' : '',
                    'queryBean.sort:' : 'id',
                    'queryBean.dir' : 'desc',
                    'queryBean.start:' : '0',
                    'queryBean.limit' : '15'
            }

            res_secondpost = s.post(url_to_dailybuild, headers=Reques_Headers_forsearch, data=form_data_forpd,
                allow_redirects=False, verify=False)
            if str(res_secondpost.status_code) != '200':
                print("error occurs in search")
                exit(1)

            # 返回的json数据格式化，并且提取我们需要的location信息
            hjson = json.loads(res_secondpost.text)
            list = hjson['list']
            if len(list) > 0:
                    list_dict = list[0]
                    outputUrl = list_dict['outputUrl']
                    #print(outputUrl)
            else:
                    print("there is no available pd images")
                    sys.exit() 

            if url_tag == "0":
                    # 将字符串进行拼接得到我们需要的下载路径
                    des_url = re.search(r'.*(?=-)', outputUrl).group(0)
                    imagedb_url = des_url + "-%5bdef%5d/image.tar.bz2"
                    imagepd_deb = des_url + "-%5bdef%5d/deb/s6120-product-package_1.0.0_x86.deb"
                    imagepd_deb2 = des_url + "-%5bdef%5d/deb/" + cap_deb
                    #print(imagedb_url)
                    #print(imagepd_deb)
                    with open("pd_url", "w+") as file:
                            file.seek(0)
                            file.truncate()
                            file.write(imagedb_url)
                            file.close()
                    with open("pd_deb", "w+") as file:
                            file.seek(0)
                            file.truncate()
                            file.write(imagepd_deb)
                            file.close()

            #print("-----------------------------")
            #print(image_url)
            #print(imagedb_url)
            #print(imagepd_deb)
            #更新images
            images_update(imagedb_url, image_url, imagepd_deb, imagepd_deb2)

        time.sleep(1)
        #print("------------get offline_debug file-----------------")
        get_rdb_file()

        start_mom()

        #使用静态或动态rdb复现
        operate_flag = input('please input repetition method(static or dynamic):')
        if operate_flag == "static":
            rdb_reload()
            print("------------now u can restart ur process to try repetition-----------------")
        elif operate_flag == "dynamic":
            rdb_reload()
            #未实现拉起发通告app#
            os.system("cp -r /data/rdb_debug/drdbframe /home/x86rgos/images/sbin && chmod +x /home/x86rgos/images/sbin/drdbframe && /home/x86rgos/images/sbin/drdbframe ")

        else:
            print('wrong repetition method!!!')    
            sys.exit()             